package com.huluobo.h3kotlinproject.category

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.huluobo.h3kotlinproject.R
import com.huluobo.h3kotlinproject.base.BaseMVVMFragment
import com.huluobo.h3kotlinproject.data.GoodsBean
import com.huluobo.h3kotlinproject.databinding.FragmentCategoryBinding
import com.huluobo.h3kotlinproject.detail.DetailActivity
import com.huluobo.h3kotlinproject.net.Const
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener

class CategoryFragment : BaseMVVMFragment<FragmentCategoryBinding>() {

    //ViewModel
    private lateinit var viewModel: CategoryViewModel

    //一级分类的适配器
    private lateinit var parentAdapter: CategoryParentAdapter

    //二级分类的适配器
    private lateinit var childAdapter: CategoryChildAdapter

    //三级列表的适配器
    private lateinit var goodsAdapter: CategoryGoodsAdapter

    //一级分类的数据源
    private val categoryParentList = mutableListOf<CategoryBean>()

    //二级分类的数据源
    private val categoryChildList = mutableListOf<CategoryBean>()

    //三级列表的数据源
    private val categoryGoodsList = mutableListOf<GoodsBean>()

    //一级分类的点击id
    private var parentCategoryId: Int = 1

    //二级分类的点击id
    private var childCategoryId = 0

    //三级列表的点击id
    private var goodCategoryId = 0

    private val pageSize = 10

    private var currentPage = 1

    override fun getLayoutId(): Int = R.layout.fragment_category

    override fun initData() {
        viewModel = ViewModelProvider(requireActivity())[CategoryViewModel::class.java]

        viewModel.getCategoryParent()

        //获取一级分类的数据变化
        viewModel.categoryParentSuccess.observe(this) {
            categoryParentList.clear()
            categoryParentList.addAll(it.data)
            parentAdapter.notifyDataSetChanged()

            //初始化时获取一级分类的第0位二级列表
            val data = categoryParentList[0].data
            if (data != null) {
                categoryChildList.clear()
                categoryChildList.addAll(data)
                childAdapter.notifyDataSetChanged()
            }

            //初始化时获取二级分类的第0位的id,请求三级列表
            childCategoryId = categoryChildList[0].id
            viewModel.getCategoryList(childCategoryId, currentPage, pageSize)
        }

        viewModel.categoryListSuccess.observe(this) {
            if (currentPage == 1) {
                categoryGoodsList.clear()
            }
            categoryGoodsList.addAll(it.data)
            goodsAdapter.notifyDataSetChanged()
        }
    }

    override fun initView() {
        //初始化一级列表
        dataBinding.categoryRvParent.layoutManager = LinearLayoutManager(requireContext())
        parentAdapter = CategoryParentAdapter(categoryParentList)
        dataBinding.categoryRvParent.adapter = parentAdapter

        parentAdapter.setOnItemClickListener { adapter, view, position ->
            parentCategoryId = categoryParentList[position].id
            parentAdapter.setSelectItemPosition(parentCategoryId)
            //点击时,切换二级列表数据
            val data = categoryParentList[position].data
            if (data != null) {
                childCategoryId = data[0].id
                categoryChildList.clear()
                categoryChildList.addAll(data)
                childAdapter.notifyDataSetChanged()

                if (categoryChildList.size != 0) {
                    viewModel.getCategoryList(categoryChildList[0].id, currentPage, pageSize)
                }
            }

        }

        //初始化二级列表
        dataBinding.categoryRvChild.layoutManager =
            GridLayoutManager(requireContext(), 2, RecyclerView.HORIZONTAL, false)
        childAdapter = CategoryChildAdapter(categoryChildList)
        dataBinding.categoryRvChild.adapter = childAdapter
        childAdapter.setOnItemClickListener { _, _, position ->

            //保存需要请求三级列表的id
            childCategoryId = categoryChildList[position].id

            //请求三级列表
            viewModel.getCategoryList(childCategoryId, currentPage, pageSize)
        }

        //初始化三级列表
        dataBinding.categoryRvGoodsList.layoutManager = GridLayoutManager(requireContext(), 2)
        goodsAdapter = CategoryGoodsAdapter(categoryGoodsList)
        dataBinding.categoryRvGoodsList.adapter = goodsAdapter
        goodsAdapter.setOnItemClickListener { adapter, view, position ->
            val intent = Intent(requireActivity(), DetailActivity::class.java)
            intent.putExtra(Const.PARAM_GOODS_ID, categoryGoodsList[position].id)
            startActivity(intent)
        }

        //初始化刷新框架
        dataBinding.categoryRefresh.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                if (refreshLayout.isRefreshing) {
                    refreshLayout.finishRefresh()
                }
                currentPage = 1
                viewModel.getCategoryList(childCategoryId, currentPage, pageSize)
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                if (refreshLayout.isLoading) {
                    refreshLayout.finishLoadMore()
                }
                currentPage += 1
                viewModel.getCategoryList(childCategoryId, currentPage, pageSize)
            }
        })
    }
}